# encoding: utf-8
import datetime
from app import scheduler
from app.extensions import db,log
from app.modules.login.models import LoginToken
from app.modules.common.utils import DbCommit
"""
定时刷新tokenid的退出状态 将超过2天的token设置为退出 添加退出时间
"""
def logoutJob():
    _app = scheduler.app
    _now = datetime.datetime.now()
    _date = _now+datetime.timedelta(days=-2)
    with _app.app_context():
        _tokens = db.session.query(LoginToken).filter(LoginToken.create_time <= _date,LoginToken.login_out_time == None).all()
        for _t in _tokens:
            log.info('app.modules.common.scheduler.logoutJob %s ' % _t.to_dict)
            _t.login_out_time = _now
            _t.desc = "超时自动退出"
        if len(_tokens) > 0:
            _rtn = DbCommit()
            log.info('app.modules.common.scheduler.logoutJob %s ' % _rtn)

